<?php

namespace app\admin\controller;

use app\common\model\Config as ConfigModel;
use app\common\validate\Config as ConfigV;
use app\common\model\ConfigCate as ConfigCateModel;
use app\common\validate\ConfigCate as ConfigCateV;

class Config extends Base
{
    public function index()
    {
        if ($this->request->isAjax()) {
            try {
                $model = new ConfigModel;
                $data = [];
                foreach ($this->post['data'] as $v) {
                    if ($v['type'] == 'checkbox') {
                        $v['value'] = implode(',', $v['value']);
                    }
                    $data[] = $v;
                }
                $model->saveAll($data);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '提交失败');
            }
            return $this->success('提交成功', 'index');
        }
        $cate_list = ConfigCateModel::with('config')->select();
        if (empty($this->get['cate_id'])) {
            $this->get['cate_id'] = $cate_list[0]['id'] ?? 0;
        }

        $config_list = ConfigModel::where(['cate_id' => $this->get['cate_id']])->select();
        return $this->fetch('', ['cate_list' => $cate_list, 'config_list' => $config_list]);
    }
    /**
     * 配置列表
     */
    public function list()
    {
        $list = ConfigModel::with('cate')->paginate([
            'list_rows'     =>  20,
            'query'         =>  $this->get
        ]);
        return $this->fetch('', ['list' => $list]);
    }

    /**
     * 添加配置
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try {
                validate(ConfigV::class)->check($this->post);
                (new ConfigModel)->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '添加失败');
            }
            return $this->success('添加成功', 'list');
        }
        $cate_list = ConfigCateModel::select();
        return $this->fetch('', ['cate_list' => $cate_list]);
    }

    /**
     * 修改配置
     */
    public function edit($id)
    {
        $info = ConfigModel::find($id);
        $info->hidden(['create_time', 'update_time']);
        if ($this->request->isAjax()) {
            try {
                validate(configV::class)->scene('edit')->check($this->post);
                $info->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '修改失败');
            }
            return $this->success('修改成功', 'list');
        }
        $cate_list = ConfigCateModel::select();
        return $this->fetch('', ['cate_list' => $cate_list, 'info' => $info]);
    }

    /**
     * 删除配置
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            try {
                ConfigModel::destroy($this->post['id']);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '删除失败');
            }
            return $this->success('删除成功', 'list');
        }
    }

    /**
     * 分组列表
     */
    public function cate_list()
    {
        $list = ConfigCateModel::paginate([
            'list_rows'     =>  20,
            'query'         =>  $this->get
        ]);
        return $this->fetch('', ['list' => $list]);
    }

    /**
     * 添加分组
     */
    public function cate_add()
    {
        if ($this->request->isAjax()) {
            try {
                validate(ConfigCateV::class)->check($this->post);
                (new ConfigCateModel)->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '添加失败');
            }
            return $this->success('添加成功', 'cate_list');
        }
        return $this->fetch('');
    }

    /**
     * 修改分组
     */
    public function cate_edit($id)
    {
        $info = ConfigCateModel::find($id);
        $info->hidden(['create_time', 'update_time']);
        if ($this->request->isAjax()) {
            try {
                validate(ConfigCateV::class)->scene('edit')->check($this->post);
                $info->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '修改失败');
            }
            return $this->success('修改成功', 'cate_list');
        }
        return $this->fetch('', ['info' => $info]);
    }

    /**
     * 删除分组
     */
    public function cate_del()
    {
        if ($this->request->isAjax()) {
            try {
                ConfigCateModel::destroy($this->post['id']);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '删除失败');
            }
            return $this->success('删除成功', 'cate_list');
        }
    }

    public function upload()
    {
        if ($this->request->isAjax()) {

            // 获取表单上传文件
            $files = request()->file();
            try {
                validate(['image' => 'filesize:2048000|fileExt:jpg,png,gif'])->check($files);
                $savename = \think\facade\Filesystem::putFile('topic', $files['image']);
            } catch (\think\exception\ValidateException $e) {
                return $this->error($e->getMessage()?:'上传失败');
            }
            return $this->success('上传成功','','/upload/'.$savename);
        }
    }
}
